package com.example;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class ExcelExporter {
    public static void exportToExcel(List<Note> notes, String outputPath) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Notes");

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("Note ID");
        headerRow.createCell(1).setCellValue("XSEC Token");
        headerRow.createCell(2).setCellValue("Display Title");
        headerRow.createCell(3).setCellValue("Page Link");

        // 填充数据
        int rowNum = 1;
        for (Note note : notes) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(note.getNoteId());
            row.createCell(1).setCellValue(note.getXsecToken());
            row.createCell(2).setCellValue(note.getDisplayTitle());

            // 生成第四列（D列）
            row.createCell(3).setCellValue(generateCustomColumn(note));
        }

        // 自动调整列宽
        for (int i = 0; i < 4; i++) {
            sheet.autoSizeColumn(i);
        }

        // 保存文件
        try (FileOutputStream outputStream = new FileOutputStream(outputPath)) {
            workbook.write(outputStream);
        }
        workbook.close();
    }

    private static String generateCustomColumn(Note note) {
        // 在这里实现你的自定义逻辑
        return "https://www.xiaohongshu.com/user/profile/62fda34d000000000f007ea3/" + note.getNoteId() + "?xsec_token=" + note.getXsecToken()+"&xsec_source=pc_user";
    }
}